﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace ExtractXMLTags
{
    class Program
    {
        public static List<string> ExtractTags(string text)
        {
            string tagPattern = @"<\w+>";
            List<string> results = new List<string>();
            MatchCollection allTags = Regex.Matches(text, tagPattern);
            foreach (Match tag in allTags)
            {
                string tagWord = tag.Value.Substring(1, tag.Value.Length - 2);
                string pattern = "<" + Regex.Escape(tagWord) + @">(.|\s)*</" + Regex.Escape(tagWord) + ">";
                MatchCollection tagMatches = Regex.Matches(text, pattern);
                foreach (Match match in tagMatches)
                {
                    results.Add(string.Format("tag={0}, value={1}", tagWord, 
                        match.Value.Substring(tagWord.Length+2, match.Value.Length-tagWord.Length*2-5)));
                }
            }
            return results;
        }

        static void Main(string[] args)
        {
            string text = @"<name><first>Ivo</first></name><age>29</age>";
            List<string> res = ExtractTags(text);
            foreach (string str in res)
            {
                Console.WriteLine(str);
            }
        }
    }
}
